class OperationsController < ApplicationController
  # GET /operations
  # GET /operations.xml
  
  before_filter :login_required
  
  def index
    @title = "Index Operations"
    @operations = Operation.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @operations }
    end
  end

  # GET /operations/1
  # GET /operations/1.xml
  def show
    @title = "Show Operations"
    @operation = Operation.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @operation }
    end
  end

  # GET /operations/new
  # GET /operations/new.xml
  def new
    @title = "New Operations"
    @operation = Operation.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @operation }
    end
  end

  # GET /operations/1/edit
  def edit
    @title = "Edit Operations"
    @operation = Operation.find(params[:id])
  end

  # POST /operations
  # POST /operations.xml
  def create
    @title = "Create Operations"
    @operation = Operation.new(params[:operation])
    if request.xhr?
      if @operation.save    
        @operation_counts_date = Operation.find(:all, :select => "count(*) as count,start_date", :group => "start_date")
        @operation_counts_status = Operation.find(:all, :select => "count(*) as count,status", :group => "status")
        render :update do |page|
          page.replace_html 'operation_graph', :partial => 'supervisions/operation_report', :locals => {:operation_counts_date => @operation_counts_date, :operation_counts_status => @operation_counts_status}
        end
      else
        render :nothing => true
      end
    else
      respond_to do |format|
        if @operation.save
          flash[:notice] = 'Operation was successfully created.'
          format.html { redirect_to(@operation) }
          format.xml  { render :xml => @operation, :status => :created, :location => @operation }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @operation.errors, :status => :unprocessable_entity }
        end
      end
    end
  end

  # PUT /operations/1
  # PUT /operations/1.xml
  def update
    @title = "Update Operations"
    @operation = Operation.find(params[:id])

    respond_to do |format|
      if @operation.update_attributes(params[:operation])
        flash[:notice] = 'Operation was successfully updated.'
        format.html { redirect_to(@operation) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @operation.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /operations/1
  # DELETE /operations/1.xml
  def destroy
    @title = "Destroy Operations"
    @operation = Operation.find(params[:id])
    @operation.destroy

    respond_to do |format|
      format.html { redirect_to(operations_url) }
      format.xml  { head :ok }
    end
  end
  
    def add_computer
    
    computer = Computer.find(params[:computer_id])
    operation = Operation.find(params[:operation_id])
    operation.computers << computer
    
    render :update do |page|
      page.remove "computer_#{params[:computer_id]}"
      page.insert_html :bottom , 'container_computer', :partial => 'operations/operation_computer', :locals => {:computer => computer}
    end
 
  end

  def delete_computer
    
    computer = Computer.find(params[:computer_id])
    operation = Operation.find(params[:operation_id])
    operation.computers.delete(computer)
      
    render :update do |page|
      page.remove "computer_#{params[:computer_id]}"
      page.insert_html :bottom , 'trash_computer', :partial => 'operations/operation_computer', :locals => {:computer => computer}
    end
    
  end
end
